package com.example.enterprise.controller;

import com.example.enterprise.entity.Picture;
import com.example.enterprise.entity.SeLogWithBLOBs;
import com.example.enterprise.service.PictureService;
import com.example.enterprise.service.SeLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

import static com.example.enterprise.util.CommunityUtil.getFiledName;

@Controller
public class LogDetailController {


    @Autowired
    private SeLogService seLogService;

    @Autowired
    private PictureService pictureService;

    @RequestMapping(path = "/log/detail/{seLogId}", method = RequestMethod.GET)
    public String getLogDetail(@PathVariable("seLogId") int seLogId, Model model) {

        SeLogWithBLOBs log = seLogService.findLogByID(seLogId);
        //反射得到属性和值
        Map<String, Object> map = getFiledName(log);

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            model.addAttribute(entry.getKey(), entry.getValue());
        }

        //寻找图片
        List<Picture> pictures=pictureService.getPicBySeLogId(seLogId);
        if(pictures.size()>0){
            model.addAttribute("pictures",pictures);
        }


        return "/site/log-detail";
    }
}
